#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int myAtoi1(string str) {
    if((str[0]>'9'||str[0]<'0')&&str[0]!='-'&&str[0]!=' ')return 0;
    int size=str.size();
    int result=0,i=0,t=0;
    char temp;
    bool isPositive=true;
    while(i<size){
        temp=str[i];
        if((temp>'9'||temp<'0')&&temp!='-'){i++;continue;}
        if(temp=='-'){isPositive=false;i++;continue;}
        if(isPositive==true){
            t=atoi(&temp);
            if(result>INT_MAX/10||(result==INT_MAX/10&&t>7))return INT_MAX;
        }
        else{
            t=-atoi(&temp);
            if(result<INT_MIN/10||(result==INT_MIN/10&&t<-8))return INT_MIN;
        }
        result=result*10+t;
        i++;
    }
    return result;
}

int myAtoi(string str) {
    int size=str.size();
    int result=0,i=0,t=0;
    string temp;
    bool isPositive=true,isFirst=true;
    while(i<size){
        temp=str[i];
        if((temp>"9"||temp<"0")&&temp!="-"&&temp!=" "&&temp!="+"&isFirst)return 0;
        if(temp==".")break;
        if(str.substr(i,2)=="+-"||str.substr(i,2)=="-+")break;
        if(str.substr(i,2)=="+ "||str.substr(i,2)=="- ")break;
        if((str[i]=='+'&&(str[i+1]>'9'||str[i+1]<'0'))
        ||(str[i]=='-'&&(str[i+1]>'9'||str[i+1]<'0')))break;
        if(temp=="-"&&isFirst){isFirst= false;isPositive=false;i++;continue;}
        else if(temp=="-"&&!isFirst){break;}
        if(temp>"9"||temp<"0"){i++;continue;}
        if(isPositive==true){
            t=atoi(temp.c_str());
            if(result>INT_MAX/10||(result==INT_MAX/10&&t>7))return INT_MAX;
        }
        else{
            t=-atoi(temp.c_str());
            if(result<INT_MIN/10||(result==INT_MIN/10&&t<-8))return INT_MIN;
        }
        isFirst= false;
        result=result*10+t;
        if((str[i+1]>'9'||str[i+1]<'0'))break;
        i++;
    }
    return result;
}
int main() {
    cout<<"myAtoi():"<<myAtoi("b11228552307")<<endl;
    cout<<INT_MAX<<INT_MIN<<endl;
    return 0;
}